package com.sino.androiddesign.recyclerviewdemo


import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.fragment_custom_rv.*


/**
 * 自定义RecyclerView
 *
 */
class CustomRvFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_custom_rv, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        fragment_custom_rv.layoutManager = LinearLayoutManager(context)
        var datas: ArrayList<String> = ArrayList()
        for (i in 0 until 30) {
            datas.add("custom $i")
        }
        var customAdapter = CustomAdapter(context!!, datas)
        fragment_custom_rv.adapter = customAdapter
        custom_add_header_btn.setOnClickListener {
            println("点击")
            var rootView = LayoutInflater.from(context).inflate(R.layout.fragment_hf_header_layout, fragment_custom_rv, false)
            fragment_custom_rv.addHeaderView(rootView)
        }

    }
}

class CustomAdapter(private var context: Context?, private var datas: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var inflate = LayoutInflater.from(context).inflate(R.layout.fragment_hf_item_layout, parent, false)
        var testViewHolder = TestViewHolder(inflate)
        testViewHolder.bindView(inflate)
        return testViewHolder;
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is TestViewHolder) {
            holder.textTv!!.text = datas[position]
        }
    }
}

/**
 * 自定义 刷新view
 */
class EasyRecyclerView : RecyclerView {

    /**
     * 必须要重写构造方法 不然会出现错误
     */
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)


    //列表数据
    private var mAdapter: Adapter<*>? = null

    private var headerAndFooterAdapter: HeaderAndFooterAdapter? = null

    //没有数据  加载数据
    private var mEmptyView: View? = null
    private var mLoadingView: View? = null

    //防止set 多次
    override fun setAdapter(adapter: Adapter<*>?) {
        if (mAdapter != null) {
            mAdapter!!.unregisterAdapterDataObserver(dataObserver)
            mAdapter = null
        }
        this.mAdapter = adapter

        headerAndFooterAdapter = adapter as? HeaderAndFooterAdapter ?: HeaderAndFooterAdapter(adapter as Adapter<ViewHolder>)
        super.setAdapter(headerAndFooterAdapter)

        mAdapter!!.registerAdapterDataObserver(dataObserver)

        if (mLoadingView != null && mLoadingView!!.visibility == View.VISIBLE) {
            mLoadingView!!.visibility = View.GONE
        }
    }

    fun addHeaderView(view: View) {
        if (headerAndFooterAdapter != null) {
            headerAndFooterAdapter!!.addHeaderView(view)
        }
    }

    fun addFooterView(view: View) {
        if (headerAndFooterAdapter != null) {
            headerAndFooterAdapter!!.addFooterView(view)
        }
    }

    fun removeHeaderView(view: View) {
        if (headerAndFooterAdapter != null) {
            headerAndFooterAdapter!!.removeHeaderView(view)
        }
    }

    fun removeFooterView(view: View) {
        if (headerAndFooterAdapter != null) {
            headerAndFooterAdapter!!.removeFooterView(view)
        }
    }

    private var dataObserver: AdapterDataObserver = object : AdapterDataObserver() {
        override fun onChanged() {
            if (mAdapter == null) return
            if (headerAndFooterAdapter != mAdapter) {
                headerAndFooterAdapter!!.notifyDataSetChanged()
            }
            dataChanged()
        }

        override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
            if (mAdapter == null) return
            // 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
            if (headerAndFooterAdapter !== mAdapter)
                headerAndFooterAdapter!!.notifyItemRemoved(positionStart)
            dataChanged()
        }

        override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
            if (mAdapter == null) return
            // 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
            if (headerAndFooterAdapter !== mAdapter)
                headerAndFooterAdapter!!.notifyItemMoved(fromPosition, toPosition)
            dataChanged()
        }


        override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
            if (mAdapter == null) return
            // 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
            if (headerAndFooterAdapter !== mAdapter)
                headerAndFooterAdapter!!.notifyItemInserted(positionStart)
            dataChanged()
        }

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
            if (mAdapter == null) return
            // 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
            if (headerAndFooterAdapter !== mAdapter)
                headerAndFooterAdapter!!.notifyItemChanged(positionStart)
            dataChanged()
        }

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
            if (mAdapter == null) return
            // 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
            if (headerAndFooterAdapter !== mAdapter)
                headerAndFooterAdapter!!.notifyItemChanged(positionStart, payload)
            dataChanged()
        }


    }

    /**
     * 添加一个空列表数据页面
     */
    fun addEmptyView(emptyView: View) {
        this.mEmptyView = emptyView
    }

    /**
     * 添加一个正在加载数据的页面
     */
    fun addLoadingView(loadingView: View) {
        this.mLoadingView = loadingView
        mLoadingView!!.visibility = View.VISIBLE
    }

    private fun dataChanged() {
        if (mAdapter!!.itemCount == 0) {
            // 没有数据
            if (mEmptyView != null) {
                mEmptyView!!.visibility = View.VISIBLE
            }
        } else {
            // 没有数据
            if (mEmptyView != null) {
                mEmptyView!!.visibility = View.GONE
            }
        }
    }
}



